﻿using uForum.Library;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using System;

namespace uForum.Test
{
    
    
    /// <summary>
    ///This is a test class for XsltTest and is intended
    ///to contain all XsltTest Unit Tests
    ///</summary>
    [TestClass()]
    public class XsltTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for ResolveLinks
        ///</summary>
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        //[TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("C:\\Documents and Settings\\Morten Bock Sørensen\\Dokumenter\\OurUmbraco\\uForum", "/")]
        //[UrlToTest("http://localhost:53747/")]
        //public void ResolveLinksTest()
        //{
        //    string body = string.Empty; // TODO: Initialize to an appropriate value
        //    string expected = string.Empty; // TODO: Initialize to an appropriate value
        //    string actual;
        //    actual = Xslt.ResolveLinks(body);
        //    Assert.AreEqual(expected, actual);
        //    Assert.Inconclusive("Verify the correctness of this test method.");
        //}

        /// <summary>
        ///A single link
        ///</summary>
        [TestMethod()]
        public void ResolveLinksInPureText()
        {
            string body = "<p>Here is some text with a link http://www.dr.dk/testing</p>";
            string expected = "<p>Here is some text with a link <a rel=\"nofollow\" href=\"http://www.dr.dk/testing\">http://www.dr.dk/testing</a></p>";
            string actual;
            actual = Xslt.ResolveLinks(body);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A single link with no http:
        ///</summary>
        [TestMethod()]
        public void ResolveLinksInPureTextNoHttp()
        {
            string body = "<p>Here is some text with a link www.dr.dk/testing</p>";
            string expected = "<p>Here is some text with a link <a rel=\"nofollow\" href=\"http://www.dr.dk/testing\">www.dr.dk/testing</a></p>";
            string actual;
            actual = Xslt.ResolveLinks(body);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A single link anchor
        ///</summary>
        [TestMethod()]
        public void ResolveLinksHasAnchor()
        {
            string body = "<p>Here is some text with a link <a href=\"http://www.dr.dk/testing\">http://www.dr.dk/testing</a></p>";
            string expected = "<p>Here is some text with a link <a rel=\"nofollow\" href=\"http://www.dr.dk/testing\">http://www.dr.dk/testing</a></p>";
            string actual;
            actual = Xslt.ResolveLinks(body);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A single link anchor with text
        ///</summary>
        [TestMethod()]
        public void ResolveLinksHasAnchorWithText()
        {
            string body = "<p>Here is some text with a link <a href=\"http://www.dr.dk/testing\">Link til dr</a></p>";
            string expected = "<p>Here is some text with a link <a rel=\"nofollow\" href=\"http://www.dr.dk/testing\">Link til dr</a></p>";
            string actual;
            actual = Xslt.ResolveLinks(body);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A single link with dirty anchor
        ///</summary>
        [TestMethod()]
        public void ResolveLinksHasDirtyAnchor()
        {
            string body = "<p>Here is some text with a link <a style=\"color: red\" href=\"http://www.dr.dk/testing\" target=\"_blank\">http://www.dr.dk/testing</a></p>";
            string expected = "<p>Here is some text with a link <a rel=\"nofollow\" href=\"http://www.dr.dk/testing\">http://www.dr.dk/testing</a></p>";
            string actual;
            actual = Xslt.ResolveLinks(body);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///Multiple links with dirty anchors
        ///</summary>
        [TestMethod()]
        public void ResolveLinksHasDirtyAnchors()
        {
            string body = "<p>Here is some text with a link <a style=\"color: red\" href=\"http://www.jp.dk/testing?test=blah#test\" target=\"_blank\">Some "+ Environment.NewLine + "Link Text</a> and another <a style=\"color: red\" href=\"http://www.dr.dk/testing\" target=\"_blank\">http://www.dr.dk/testing</a> wuhuu</p>";
            string expected = "<p>Here is some text with a link <a rel=\"nofollow\" href=\"http://www.jp.dk/testing?test=blah#test\">Some " + Environment.NewLine + "Link Text</a> and another <a rel=\"nofollow\" href=\"http://www.dr.dk/testing\">http://www.dr.dk/testing</a> wuhuu</p>";
            string actual;
            actual = Xslt.ResolveLinks(body);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///Multiple equal links with dirty anchors
        ///</summary>
        [TestMethod()]
        public void ResolveLinksHasDirtyEqualAnchors()
        {
            string body = "<p>Here is some text with a link <a style=\"color: red\" href=\"http://www.dr.dk/testing\" target=\"_blank\">http://www.dr.dk/testing</a> and another <a style=\"color: red\" href=\"http://www.dr.dk/testing\" target=\"_blank\">http://www.dr.dk/testing</a> wuhuu</p>";
            string expected = "<p>Here is some text with a link <a rel=\"nofollow\" href=\"http://www.dr.dk/testing\">http://www.dr.dk/testing</a> and another <a rel=\"nofollow\" href=\"http://www.dr.dk/testing\">http://www.dr.dk/testing</a> wuhuu</p>";
            string actual;
            actual = Xslt.ResolveLinks(body);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///Uppercase tags and attributes
        ///</summary>
        [TestMethod()]
        public void ResolveLinksUppercase()
        {
            string body = "<p>Here is some text with a link <A STYLE=\"color: red\" HREF=\"http://www.dr.dk/testing\" TARGET=\"_blank\">http://www.dr.dk/testing</A> and another <a style=\"color: red\" href=\"http://www.dr.dk/testing\" target=\"_blank\">http://www.dr.dk/testing</a> wuhuu</p>";
            string expected = "<p>Here is some text with a link <a rel=\"nofollow\" href=\"http://www.dr.dk/testing\">http://www.dr.dk/testing</a> and another <a rel=\"nofollow\" href=\"http://www.dr.dk/testing\">http://www.dr.dk/testing</a> wuhuu</p>";
            string actual;
            actual = Xslt.ResolveLinks(body);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A long single link
        ///</summary>
        [TestMethod()]
        public void ResolveLinksLongUrl()
        {
            string body = "<p>Here is some text with a link http://www.thisisareallylongdomain.dk/withAreall/yreally/longpageaddress?and=test&afewparameters=testing#answer</p>";
            string expected = "<p>Here is some text with a link <a rel=\"nofollow\" href=\"http://www.thisisareallylongdomain.dk/withAreall/yreally/longpageaddress?and=test&afewparameters=testing#answer\">www.thisisareallylongdomain.dk/.../longpageaddress</a></p>";
            string actual;
            actual = Xslt.ResolveLinks(body);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A single link with % signs
        ///</summary>
        [TestMethod()]
        public void ResolveLinksWithUrlEncodedValues()
        {
            string body = "<p>With <strong>CWS </strong>being my friendly URL.<br />As my current project url http://wifi.umbraco.org/projects/creative-website-starter-%28cws%29 is possibly a bit long.</p>";
            string expected = "<p>With <strong>CWS </strong>being my friendly URL.<br />As my current project url <a rel=\"nofollow\" href=\"http://wifi.umbraco.org/projects/creative-website-starter-%28cws%29\">wifi.umbraco.org/.../creative-website-starter-(cws)</a> is possibly a bit long.</p>";
            string actual;
            actual = Xslt.ResolveLinks(body);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///Don't touch links inside pre tags
        ///</summary>
        [TestMethod()]
        public void ResolveLinksExcludePreTags()
        {
            string body = "<p>Code: <pre>&lt;dingo&gt;test&lt;/dingo&gt; &lt;a href=\"http://yahoo.com\"&gt;ahahsdjasd&lt;/a&gt;</pre> text: <a href=\"http://dr.dk/test\">test</a> </p>";
            string expected = "<p>Code: <pre>&lt;dingo&gt;test&lt;/dingo&gt; &lt;a href=\"http://yahoo.com\"&gt;ahahsdjasd&lt;/a&gt;</pre> text: <a rel=\"nofollow\" href=\"http://dr.dk/test\">test</a> </p>";
            string actual;
            actual = Xslt.ResolveLinks(body);
            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///Don't touch links inside multiple pre tags
        ///</summary>
        [TestMethod()]
        public void ResolveLinksExcludeMultiplePreTags()
        {
            string body = "<p>Code: <pre>&lt;dingo&gt;test" + Environment.NewLine + "&lt;/dingo&gt; &lt;a href=\"http://yahoo.com\"&gt;ahahsdjasd&lt;/a&gt;</pre> text: <a href=\"http://dr.dk/test\">test</a> more code: <pre>public void Test(){string test = \"http://www.dr.dk/test\"; string test2 = \"&lt;a href=\"http://dr.dk/test\"&gt;test&lt;/a&gt;\";}</pre> </p>";
            string expected = "<p>Code: <pre>&lt;dingo&gt;test" + Environment.NewLine + "&lt;/dingo&gt; &lt;a href=\"http://yahoo.com\"&gt;ahahsdjasd&lt;/a&gt;</pre> text: <a rel=\"nofollow\" href=\"http://dr.dk/test\">test</a> more code: <pre>public void Test(){string test = \"http://www.dr.dk/test\"; string test2 = \"&lt;a href=\"http://dr.dk/test\"&gt;test&lt;/a&gt;\";}</pre> </p>";
            string actual;
            actual = Xslt.ResolveLinks(body);
            Assert.AreEqual(expected, actual);
        }
    }
}
